<?php
/**
 * CK框架 文件上传类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class upload
{
	/**
	 * 缓存目录
	 * @var string
	 */
	private $_saveDir='file/';
	/**
	 * 允许上传的文件名
	 * @var array
	 */
	private $_allowExts = array('jpg','jpeg','gif','png');
	/**
	 * 允许类型检查
	 * @var boolean
	 */
	private $_allowSafe = true;
	/**
	 * 文件后缀名
	 * @var string
	 */
	private $_ext = '';
	/**
	 * 错误信息数组
	 * @var array
	 */
	private $_errorArr = 
	array(
		1 => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。',
		2 => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。',
		3 => '文件只有部分被上传。',
		4 => '没有文件被上传。',
		5 => '找不到临时文件夹。',
		6 => '文件写入失败。'
	);
	/**
	 * 上传最大大小 (K为单位 1M=1024K)
	 * @var int
	 */
	private $_maxSize = 100;
	/**
	 * 设置保存的文件夹
	 * @param string $dirName
	 */
	public function setSaveDir($dirName)
	{
		$this->_saveDir = $dirName;
	}
	/**
	 * 设置允许上传的文件后缀数组
	 * @param array $arr
	 */
	public function setAllowExts($arr)
	{
		$this->_allowExts = $arr;
	}
	/**
	 * 设置是否允许安全类型检查
	 * @param boolean $b
	 */
	public function setAllowSafe($b = true)
	{
		$this->_allowSafe = $b;
	}
	/**
	 * 返回上传文件的后缀名
	 * @return string
	 */
	public function getExt()
	{
		return $this->_ext;
	}
	/**
	 * 上传文件
	 * 上传错误会抛出异常
	 * 上传成功返回文件地址
	 * @param string $uploadFile 上传的地址数组信息  为$_FILES['uploadName']数组
	 * @param string $copyFile 上传成功后复制到的地址 为空的话采用自动生成地址
	 * @throws not_File_upload_exception
	 * @throws error_upload_exception
	 * @throws safe_check_upload_exception
	 * @throws copy_file_upload_exception
	 * @return string
	 */
	public function move($uploadFile,$copyFile='')
	{
		if(!$uploadFile)
		{
			throw new not_File_upload_exception();
		}
		if($uploadFile['error'])
		{
			throw new error_upload_exception($this->_errorArr[$uploadFile['error']]);
		}
		//判断后缀
		$temp_arr = explode('.', $uploadFile['name']);
		$file_ext = array_pop($temp_arr);
		$this->_ext = strtolower(trim($file_ext));
		if($this->_allowExts && !in_array($this->_ext, $this->_allowExts))
		{
			throw new safe_check_upload_exception(lang::getLang('__FILE_IS_NOT_UPLOAD_FILE_EXT__'));
		}
		//安全类型检查
		if($this->_allowSafe)
		{
			if(in_array($this->_ext, array('jpeg','jpg','gif','png')))
			{
				if(!$this->picScan($uploadFile['tmp_name']))
				{
					throw new safe_check_upload_exception(lang::getLang('__FILE_IS_NOT_A_TYPE_FILE__',array($this->_ext)));
				}
			}
		}
		!$copyFile ? $copyFile = $this->createFilePath() : '';
		$copyFile = $this->_saveDir.'/'.$copyFile;
		dir::mkDir(dirname($copyFile));
		//Copy
		if(!move_uploaded_file($uploadFile['tmp_name'],$copyFile))
		{
			throw new copy_file_upload_exception(lang::getLang('__UPLOAD_FILE_FAILD__',array($copyFile)));
		}
		return $copyFile;
	}
	/**
	 * 检查文件是否是一个图片
	 * @param string $file
	 * @return boolean
	 */
	private function picScan($file)
	{
		$tmpImagesize = @getimagesize($file);
		list($tmpWidth, $tmpHeight, $tmpType) = (array)$tmpImagesize;
		$tmpSize = $tmpWidth * $tmpHeight;
		if($tmpSize > 16777216 || $tmpSize < 4 || empty($tmpType) || strpos($tmpImagesize['mime'], 'flash') > 0) 
		{
			return false;
		}
		return true;
	}
	/**
	 * 构建上传路劲
	 */
	private function createFilePath()
	{
		return date(ym).'/'.date(d).'/'.SYSTIME.rand(1, 100).'.'.$this->_ext;
	}
}

/**
 * CK框架 没有文件上传异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */

class not_File_upload_exception extends Exception
{
	public function __construct()
	{
		parent::__construct(lang::getLang('__HAS_NOT_FILE_UPLOAD__'));
	}
}

/**
 * CK框架 上传错误异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class error_upload_exception extends Exception
{
	
}

/**
 * CK框架 安全检查异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class safe_check_upload_exception extends Exception
{

}

/**
 * CK框架 复制文件异常
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class copy_file_upload_exception extends Exception
{
	
}